package com.weilai.util;

import com.weilai.service.EmailService;
import com.weilai.service.serviceImpl.EmailServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Random;

public class EmailUtil {

    public static void sendCode(String email, HttpServletRequest request) {
        HttpSession session = request.getSession();
        EmailService emailService = new EmailServiceImpl();
        int code;
        Random random = new Random();
        code = random.nextInt(9000);
        code = code + 1000;
        if(session.getAttribute("code") != null){
            session.removeAttribute("code");
        }
        if(session.getAttribute("CodeExpireTime") != null){
            session.removeAttribute("CodeExpireTime");
        }
        session.setAttribute("Code", code);
        session.setAttribute("CodeExpireTime", System.currentTimeMillis() + 10 * 60 * 1000);
        //发送验证码
        emailService.sendMail(email, "【haven】",
                "【haven】验证码" + code + ",您正在登录haven,若非本人操作,请勿泄露,验证码在10分钟内有效");
    }
}
